
package battleroom;

import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.bounding.BoundingBox;
import com.jme3.font.BitmapFont;
import com.jme3.font.BitmapText;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.material.RenderState.FaceCullMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Quad;
import com.jme3.system.AppSettings;
import com.jme3.system.Timer;
import com.jme3.ui.Picture;

/**
 *
 * @author Adam
 */
public class HUDGui extends AbstractAppState{
    private BitmapFont hudFont;
    
    private float jumpForceWidth, jumpForcePadding, jumpForceHeight, jumpForceBarHeight;
    private Geometry jumpForceBg;
    private Geometry jumpForceBar;
    private boolean showJumpForceBar = false;
    private BitmapText jumpForceText;
    
    private final Node guiNode;
    private final AppSettings settings;
    private final AssetManager assetManager;
    private AppStateManager stateManager;
    private PlayerState playerState;
    
    private Geometry    jumpStateFree;
    private ColorRGBA   jumpStateColor;
    private ColorRGBA   jumpStateAtColor;
    private ColorRGBA   origAtColor;
    private Geometry jumpStateAttached;
    
    public HUDGui(AppSettings settings, Node guiNode, AssetManager am)
    {
        this.guiNode = guiNode;
        this.settings = settings;
        this.assetManager = am;
        
        
    }
    
    @Override
    public void initialize(AppStateManager stateManager, Application app)
    {
        super.initialize(stateManager, app);
        this.stateManager = stateManager;
        playerState = stateManager.getState(PlayerState.class);
        hudFont = assetManager.loadFont("Interface/Fonts/CirculaThin.fnt");
        
        initMoveStateIndicator();
        initJumpForce();
    }
    
    private boolean jumpStateAdd = true; //true = add false = subtract
    private float minJmpAlpha = 0.2f;
    private float maxJmpAlpha = 0.9f;
    private float waitTime   = 1f;
    private float atWaitTime = 5f;
    private float timer       = 0.0f;
    @Override
    public void update(float tpf)
    {
        
        super.update(tpf);
        
        if(!this.isInitialized() || !this.isEnabled()) return;
        
        //Attach/detach hud_jmpstatefree
        if(guiNode.getChild("hud_jmpstatefree") == null && !playerState.isAttached() )
        {
            guiNode.attachChild(jumpStateFree);
            guiNode.detachChild(jumpStateAttached);
        }else if(playerState.isAttached() && guiNode.getChild("hud_jmpstatefree") != null)
        {
            guiNode.detachChild(jumpStateFree);
            guiNode.attachChild(jumpStateAttached);
            timer = 0.0f;
        }        
        
        //Fade hud_jmpstateattached after a time
        if(guiNode.getChild("hud_jmpstateattached") != null)
        {
            timer += tpf;
            
            if(timer > atWaitTime)
            {
                if(jumpStateAtColor.a < 0.1f)
                {
                    jumpStateAtColor = origAtColor.clone();                    
                    jumpStateAttached.getMaterial().setColor("Color", jumpStateAtColor);
                    guiNode.detachChild(jumpStateAttached);
                    timer = 0.0f;
                    System.out.println("A = " + jumpStateAtColor.a);
                }else{
                    jumpStateAtColor.a -= tpf;
                }
            }
        }
        
        
        //Pulsate hud_jmpstatefree's color
        if(guiNode.getChild("hud_jmpstatefree") != null)
        {            
            if(jumpStateColor.a == maxJmpAlpha)
            {
                timer += tpf;
                
                if(timer > waitTime)
                {
                    jumpStateAdd = false;
                    timer = 0.0f;
                }
            }else if(jumpStateColor.a == minJmpAlpha){
                jumpStateAdd = true;
            }

            if(jumpStateAdd)
            {
                jumpStateColor.a += (float)Math.sin(jumpStateColor.a * 1.5f * tpf); 
            }else{
                jumpStateColor.a -= (float)Math.sin(jumpStateColor.a * 1.5f * tpf);                
            }

            if(jumpStateColor.a > maxJmpAlpha)
            {
                jumpStateColor.a = maxJmpAlpha;
            }else if(jumpStateColor.a < minJmpAlpha)
            {
                jumpStateColor.a = minJmpAlpha;                
            }
            
            jumpStateFree.getMaterial().setColor("Color", jumpStateColor);
        }
        
    }
    
    private void initMoveStateIndicator()
    {
        origAtColor = new ColorRGBA(.7f, .6f, 0.0f, .8f);
        jumpStateColor = origAtColor.clone();
        float width = 50;
        float height = 36;
        Quad jumpStateQ = new Quad(width, height);
        Material jumpStateFreeMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        jumpStateFreeMat.setTexture("ColorMap", assetManager.loadTexture("Textures/hud/status-floating.png"));
        jumpStateFreeMat.setColor("Color", jumpStateColor);
        jumpStateFreeMat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
        jumpStateFree = new Geometry("hud_jmpstatefree", jumpStateQ);
        jumpStateFree.setMaterial(jumpStateFreeMat);
        jumpStateFree.setLocalTranslation((settings.getWidth()/2)- (width/2), settings.getHeight() - height - 20, 0);
        
        width = 50;
        height = 63;
        jumpStateAtColor = new ColorRGBA(.0f, .6f, .0f, .8f);
        Quad jumpStateAtQ = new Quad(width, height);
        Material jumpStateAttachedMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        jumpStateAttachedMat.setTexture("ColorMap", assetManager.loadTexture("Textures/hud/status-attached.png"));
        jumpStateAttachedMat.setColor("Color", jumpStateAtColor);
        jumpStateAttachedMat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
        jumpStateAttached = new Geometry("hud_jmpstateattached", jumpStateAtQ);
        jumpStateAttached.setMaterial(jumpStateAttachedMat);
        jumpStateAttached.setLocalTranslation((settings.getWidth()/2) - (width/2), settings.getHeight() - height - 20, 0);
    }
    
    private void initJumpForce()
    {
        
        float jfFontSize =12;
        jumpForceText = new BitmapText(hudFont, false);
        jumpForceText.setSize(jfFontSize);
        jumpForceText.setColor(ColorRGBA.White);
        jumpForceText.setText("0%");
        
        jumpForceWidth = 100;
        jumpForcePadding = 10;        
        jumpForceBarHeight = 10;
        jumpForceHeight = jumpForcePadding+(jumpForcePadding/2) + jumpForceBarHeight + jfFontSize; //Padding on top of bar, below bar and then below the text
        
        Quad jumpForceBgQ = new Quad(jumpForceWidth, jumpForceHeight);
        Quad jumpForceBarQ = new Quad(0, jumpForceBarHeight);
        
        Material jfBgMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        jfBgMat.setColor("Color", new ColorRGBA(.5f, .5f, .5f, .4f));
        jfBgMat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
        
        jumpForceBg = new Geometry("hud_jumpforcebg", jumpForceBgQ);
        jumpForceBg.setLocalTranslation(settings.getWidth()/2 - jumpForceWidth/2, jumpForceHeight + jumpForcePadding, 0);
        jumpForceBg.setMaterial(jfBgMat);
        
        Material jfFgMat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        jfFgMat.setColor("Color", new ColorRGBA(.8f, .6f, .2f, .8f));
        jfFgMat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
        
        Vector3f jfFgLoc = jumpForceBg.getLocalTranslation().clone();
        jfFgLoc.setX(jfFgLoc.getX() + jumpForcePadding/2);
        jfFgLoc.setY(jfFgLoc.getY() + jumpForceHeight - jumpForcePadding/2 - jumpForceBarHeight);
        jumpForceBar = new Geometry("hud_jumpforcefg", jumpForceBarQ);
        jumpForceBar.setLocalTranslation(jfFgLoc);
        jumpForceBar.setMaterial(jfFgMat);
        
        Vector3f jfTextLoc = jumpForceBg.getLocalTranslation().clone();
        jfTextLoc.setX(jfTextLoc.getX()+(jumpForcePadding/2));
        jfTextLoc.setY(jfTextLoc.getY()+(jumpForcePadding/2)+jfFontSize);
        jumpForceText.setLocalTranslation(jfTextLoc);
    }
    
    public void updateJumpForceIndicator(float percent)
    {
        jumpForceBar.setMesh(new Quad((jumpForceWidth - jumpForcePadding) * percent, jumpForceBarHeight));
        jumpForceText.setText((int)(percent*100) + "%");
    }
    
    public void toggleJumpForceIndicator(boolean toggle)
    {
        if(toggle)
        {            
            guiNode.attachChild(jumpForceBg);
            guiNode.attachChild(jumpForceText);            
            guiNode.attachChild(jumpForceBar);
        }else{
            guiNode.detachChild(jumpForceBg);
            guiNode.detachChild(jumpForceText);            
            guiNode.detachChild(jumpForceBar) ;                       
        }
    }

    boolean getJumpForceIndicatorShowing() {
        return guiNode.hasChild(jumpForceBg);
    }
}
